import {getRequest} from './api'

export const initMenu = (router,store)=>{
        if(store.state.routes.length>0){
            return;
        }

        getRequest('/system/cfg/menu').then(data=>{
            if(data){
                //格式化路由
                //console.log(data)
                let fmtRoutes = formatRoutes(data);
                //console.log(fmtRoutes)
                //添加到router
                router.addRoutes(fmtRoutes);
                //将数据存入vuex
                store.commit('initRoutes',fmtRoutes);
                //连接WebSocket
                store.dispatch('connect');

            }
        })
}

export const formatRoutes = (routes)=>{
        let fmtRoutes = [];
    routes.forEach(routes=>{
            let {
                path,
                component,
                name,
                iconCls,
                children
            } = routes;
            if(children && children instanceof Array){
                //递归
                children = formatRoutes(children);
                //console.log(children)
            }
            let fmRouter = {
                path:path,
                name:name,
                iconCls:iconCls,
                children:children,
                component(resolve){
                    if(component.startsWith('Home')){
                        //import(`../view/${component}.vue`)
                        require(['../view/'+component+'.vue'],resolve);
                    }else if(component.startsWith('Emp')){
                        //require 方式也可以  不过component 方法需要加上参数 resolve
                        require(['../view/emp/'+component+'.vue'],resolve);
                        //import('../view/emp/'+component+'.vue')
                    }else if (component.startsWith('Per')){
                        require(['../view/per/'+component+'.vue'],resolve);
                        //import(`../view/per/${component}.vue`)
                    }else if (component.startsWith('Sal')){
                        require(['../view/sal/'+component+'.vue'],resolve);
                        //import(`../view/sal/${component}.vue`)
                    }else if (component.startsWith('Sta')){
                        require(['../view/sta/'+component+'.vue'],resolve);
                        //import(`../view/sta/${component}.vue`)
                    }else if (component.startsWith('Sys')){
                        require(['../view/sys/'+component+'.vue'],resolve);
                        //import(`../view/sys/${component}.vue`)
                    }else if (component.startsWith('Sect')){
                        require(['../view/sect/'+component+'.vue'],resolve);
                        //import(`../view/sect/${component}.vue`)
                    }else if (component.startsWith('Sperson')){
                        require(['../view/sperson/'+component+'.vue'],resolve);
                        //import(`../view/sperson/${component}.vue`)
                    }else if(component.startsWith("DemonBeast")){
                        require(['../view/demonBeast/'+component+'.vue'],resolve);
                        //import(`../view/demonBeast/${component}.vue`)
                    }

                }
            };
            fmtRoutes.push(fmRouter);
        });
        return fmtRoutes;
}